//
//  MFWAppVersionTracker.m
//  MFWSdk
//
//  Created by Song Xiaofeng on 13-7-23.
//  Copyright (c) 2013年 Mafengwo Inc. All rights reserved.
//

#import "MFWAppVersionTracker.h"
#import "MFWVersionUtils.h"

NSString  *const kSDK_Notify_AppDidupgraded = @"kSDK_Notify_AppDidupgraded";
NSString  *const kSDK_NotifyInfo_Str_PreviousAppVersion = @"kSDK_NotifyInfo_Str_PreviousAppVersion";
NSString  *const kSDK_NotifyInfo_Str_NewAppVersion = @"kSDK_NotifyInfo_Str_NewAppVersion";

NSString  *const kSDK_UserDefaults_AppVersionMarker = @"kUserDefaults_AppVersionMark";

#define VERSION_SEPARATOR @","

@implementation MFWAppVersionTracker
+ (void)appLaunched;
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *userVersionRoadMap = [userDefaults stringForKey:kSDK_UserDefaults_AppVersionMarker];
    NSString *bundleAppVersion = [MFWVersionUtils appVersion];
    if (userVersionRoadMap == nil || [userVersionRoadMap length] == 0 )
    {
        [userDefaults setValue:bundleAppVersion forKey:kSDK_UserDefaults_AppVersionMarker];
        [userDefaults synchronize];// saving
        return;
    }
    
    NSString *lastVerstion = [self userLastVersionValue];
    
    NSComparisonResult result = [MFWVersionUtils compareOldVersion:lastVerstion toNewVersion:bundleAppVersion];
//    NSAssert(result != NSOrderedDescending, @"version number descending!!! something error occurs(l:%@->n:%@)", lastVerstion, bundleAppVersion);
    if (result == NSOrderedSame)
    {
        // do nothing
    }
    else if (result == NSOrderedAscending)
    {
        // 版本升级啦
        NSString *oldAppVersion = lastVerstion;
        NSString *curAppVersion = bundleAppVersion;
        
        NSString *previousAppVersion = oldAppVersion;
        
        userVersionRoadMap = [NSString stringWithFormat:@"%@%@%@", userVersionRoadMap, VERSION_SEPARATOR, curAppVersion];
        
        [userDefaults setValue:userVersionRoadMap forKey:kSDK_UserDefaults_AppVersionMarker];
        [userDefaults synchronize];// saving
        
        // broadcast upgraded notification
        [[NSNotificationCenter defaultCenter] postNotificationName:kSDK_Notify_AppDidupgraded
                                                            object:nil
                                                          userInfo:
                                @{kSDK_NotifyInfo_Str_PreviousAppVersion: previousAppVersion,
                                       kSDK_NotifyInfo_Str_NewAppVersion: bundleAppVersion}];
    }
    
}

+ (NSString*)userVersionRoadMap
{
    return [[NSUserDefaults standardUserDefaults] stringForKey:kSDK_UserDefaults_AppVersionMarker];
}

+ (NSString*)userLastVersionValue
{
    NSString *userRoadMapString = [self userVersionRoadMap];
    if (userRoadMapString == nil || [userRoadMapString length] == 0)
    {
        return nil;
    }
    
    NSArray *roadMapSequence = [userRoadMapString componentsSeparatedByString:VERSION_SEPARATOR];
    if ([roadMapSequence count] > 0)
    {
        return [roadMapSequence lastObject];
    }
    
    return nil;
}


+ (NSString*)previousAppVersion;
{
    NSString *userRoadMapString = [self userVersionRoadMap];
    if (userRoadMapString == nil || [userRoadMapString length] == 0)
    {
        return nil;
    }
    NSArray *roadMapSequence = [userRoadMapString componentsSeparatedByString:VERSION_SEPARATOR];
    if ([roadMapSequence count] > 2)
    {
        return [roadMapSequence objectAtIndex:[roadMapSequence count] - 2];
    }
    
    return nil;
}
@end
